﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnPhatTrienPhanMem.Models;

namespace DoAnPhatTrienPhanMem.Controllers
{
    public class BanHanhChinhController : BaseController
    {
        //
        // GET: /BanHanhChinh/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Details()
        {
            var ID = Request["ID"];
            ViewData["ID"] = ID;
            ViewData["TenBan"] = helper.GetOne<BanHanhChinh>(s => s.ID.ToString() == ID).TenBan;
            return View();
        }

        public ActionResult AddOrEdit()
        {
            ViewData["ID"] = "";
            var ID = Request["ID"];
            if (!String.IsNullOrEmpty(ID)) ViewData["ID"] = ID;
            return View();
        }

        public JsonResult GetAll()
        {
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<BanHanhChinhOverView>().OrderBy(s => s.TenBan).ToList();
            var tochuc = helper.GetTable<ToChucCanBo>().ToList();
            foreach (var ban in resultList)
            {
                ban.SLcanbo = tochuc.Count(s => s.MaPhongBan == ban.MaBan && s.ThuocBanVien == "BAN");
            }
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetByID()
        {
            var ID = Request["ID"];
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<BanHanhChinh>().Where(s => s.ID.ToString() == ID).ToList();
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDetailsByID()
        {
            var ID = Request["ID"];
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<BanHanhChinhDetailsView>().Where(s => s.ID.ToString() == ID).ToList();
            //var ban = helper.GetOne<BanHanhChinhDetailsView>(s => s.ID.ToString() == ID);
            var tochuc = helper.GetTable<ToChucCanBoChucDanhView>().ToList();
            foreach (var ban in resultList)
            {
                tochuc = tochuc.Where(s => s.MaPhongBan == ban.MaBan && s.ThuocBanVien == "BAN").ToList();
                ban.SLcanbo = tochuc.Count();
                ban.GS = tochuc.Count(s => s.HocHam == Constants.Constants.ChucDanh.HocHam.GIAOSU);
                ban.PGS = tochuc.Count(s => s.HocHam == Constants.Constants.ChucDanh.HocHam.PHOGIAOSU);
                ban.TSKH = tochuc.Count(s => s.HocVi == Constants.Constants.ChucDanh.HocVi.TIENSIKHOAHOC);
                ban.TS = tochuc.Count(s => s.HocHam == Constants.Constants.ChucDanh.HocVi.TIENSI);
                ban.PTS = tochuc.Count(s => s.HocHam == Constants.Constants.ChucDanh.HocVi.PHOTIENSI);
                ban.THS = tochuc.Count(s => s.HocHam == Constants.Constants.ChucDanh.HocVi.THACSI);
                ban.Truong = "";
                ban.Pho = "";
                var truong = helper.GetOne<ToChucCanBo>(s => s.ThuocBanVien == "BAN" && s.MaPhongBan == ban.MaBan && s.ViTri == Constants.Constants.ViTri.TRUONG);
                if (truong != null)
                {
                    var canbo = helper.GetOne<SoYeuCanBo>(s => s.MaCB == truong.MaCB);
                    ban.Truong = canbo.HocHam + " " + canbo.HocVi + " " + canbo.HoTen;
                }
                var pho = helper.GetOne<ToChucCanBo>(s => s.ThuocBanVien == "BAN" && s.MaPhongBan == ban.MaBan && s.ViTri == Constants.Constants.ViTri.PHO);
                if (pho != null)
                {
                    var canbo = helper.GetOne<SoYeuCanBo>(s => s.MaCB == truong.MaCB);
                    ban.Pho = canbo.HocHam + " " + canbo.HocVi + " " + canbo.HoTen;
                }
            }
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Save(FormCollection values)
        {
            var result = new Dictionary<String, Object>();

            try
            {
                var ID = values["ID"];
                var entity = String.IsNullOrEmpty(ID)
                             ? new BanHanhChinh()
                             : helper.GetOne<BanHanhChinh>(s => s.ID.ToString() == ID);
                if (entity == null)
                {
                    return WarningMessage("Không tồn tại");
                }
                // Load data to entity
                entity = (BanHanhChinh)GetDataFromForm(entity, values);
                if (entity.ID == Guid.Empty)
                {
                    #region Insert
                    if (helper.EntityExists<BanHanhChinh>(s => s.TenBan == entity.TenBan))
                    {
                        return WarningMessage("Tên đã tồn tại");
                    }
                    entity.ID = Guid.NewGuid();
                    helper.Insert(entity);
                    #endregion
                }
                else
                {
                    #region Update
                    if (helper.EntityExists<BanHanhChinh>(s => s.TenBan == entity.TenBan && s.ID != entity.ID))
                    {
                        return WarningMessage("Tên đã tồn tại");
                    }
                    helper.Update(entity, s => s.ID.ToString() == ID);
                    #endregion
                }
                result.Add("success", true);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }
        }

        public JsonResult Delete(Guid ID)
        {
            IDictionary<String, Object> result = new Dictionary<String, Object>();
            try
            {
                var SanPham = helper.GetOne<BanHanhChinh>(s => s.ID == ID);
                if (SanPham == null)
                {
                    return WarningMessage("Ban không tồn tại");
                }
                helper.Delete<BanHanhChinh>(s => s.ID == ID);
                result.Add("success", true);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }
            return Json(result);
        }

        public BanHanhChinh GetBanByID(String ID)
        {
            var data = new BanHanhChinh();
            try
            {
                data = helper.GetOne<BanHanhChinh>(s => s.ID.ToString() == ID);
            }
            catch
            {
            }
            return data;
        }


    }
}
